/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sniper.springmvc.config;

import com.sniper.springmvc.utils.PropertiesUtil;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 全局配置类
 *
 * @author ThinkGem
 * @version 2013-03-23
 */
public class Global {

    /**
     * 属性文件加载对象
     */

    private static final String configPath = "properties/sniper.properties";
    private static final Map<String, String> values;

    static {
        InputStream in = Global.class.getClassLoader().getResourceAsStream(configPath);
        PropertiesUtil pu = new PropertiesUtil(in);
        values = pu.getValues();
    }

    /**
     * 下面写法不能改变实时读取，因为已经第一次初始化
     */
    private static Map<String, String> sniper = new HashMap<>();

    static {
        Properties properties = new Properties();
        String path = Global.class.getClassLoader().getResource(configPath).getPath();
        InputStream is = null;
        try {
            is = new FileInputStream(new File(path));
            properties.load(is);
            Enumeration names = properties.propertyNames();
            while (names.hasMoreElements()) {
                String key = (String) names.nextElement();
                String value = properties.getProperty(key);
                sniper.put(key, value);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static Map<String, String> getValues() {
        return values;
    }

    public static Map<String, String> getSniper() {
        return sniper;
    }

    /**
     * 可以实时获取修改过的配置文件
     *
     * @return
     */
    public static Map<String, String> getMap() {
        Properties properties = new Properties();
        String path = Global.class.getClassLoader().getResource(configPath).getPath();
        InputStream is = null;
        try {
            is = new FileInputStream(new File(path));
            properties.load(is);
            Enumeration names = properties.propertyNames();
            Map<String, String> map = new HashMap<>();
            while (names.hasMoreElements()) {
                String key = (String) names.nextElement();
                String value = properties.getProperty(key);
                map.put(key, value);
            }
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return null;
    }

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        if (values.containsKey(key)) {
            return values.get(key);
        }
        return null;
    }


    /**
     * 获取管理端根路径
     */
    public static String getAdminPath() {
        return getConfig("adminPath");
    }

    /**
     * 获取前端根路径
     */
    public static String getFrontPath() {
        return getConfig("frontPath");
    }

    /**
     * 获取URL后缀
     */
    public static String getUrlSuffix() {
        return getConfig("urlSuffix");
    }

    /**
     * 是否是演示模式，演示模式下不能修改用户、角色、密码、菜单、授权
     */
    public static Boolean isDemoMode() {
        String dm = getConfig("demoMode");
        return "true".equals(dm) || "1".equals(dm);
    }

    /**
     * 在修改系统用户和角色时是否同步到Activiti
     */
    public static Boolean isSynActivitiIndetity() {
        String dm = getConfig("activiti.isSynActivitiIndetity");
        return "true".equals(dm) || "1".equals(dm);
    }


}